//go:build cgo
// +build cgo

/*
 * @Author: Wangjun
 * @Date: 2022-07-22 15:27:01
 * @LastEditTime: 2023-03-16 22:52:56
 * @LastEditors: Wangjun
 * @Description:
 * @FilePath: \golib\winextend\c\main.go
 * hnxr
 */
package main

import (
	"log"
	"time"

	"gitee.com/haodreams/golib/winextend"
	"github.com/lxn/win"
)

func main() {
	for {
		do()
		time.Sleep(time.Second * 5)
	}
}

func do() {
	var point win.POINT
	ok := win.GetCursorPos(&point)
	if !ok {
		log.Println("获取位置错误")
		return
	}
	hwnd, err := winextend.WindowFromPoint(int(point.X), int(point.Y))
	if err != nil {
		log.Println(err)
		return
	}
	if hwnd == 0 {
		log.Println("无效的句柄")
		return
	}

	title := winextend.GetWindowText(hwnd)
	var rect win.RECT
	win.GetWindowRect(hwnd, &rect)

	topHwnd := winextend.GetTopWinowd(hwnd)
	if topHwnd == 0 {
		log.Println("无父窗口")
		return
	}
	var parentRect win.RECT
	ok = win.GetWindowRect(topHwnd, &parentRect)
	if !ok {
		log.Println("获取窗体大小错误")
		return
	}
	parentTitle := winextend.GetWindowText(topHwnd)

	log.Printf("%x parent title:%s;title:%s;parent rect:(%d,%d,%d,%d);rect:(%d,%d,%d,%d);abs pos:(%d,%d);pos:(%d,%d)\n", hwnd,
		parentTitle, title, parentRect.Left, parentRect.Top, parentRect.Right, parentRect.Bottom,
		rect.Left, rect.Top, rect.Right, rect.Bottom, point.X, point.Y, point.X-parentRect.Left, point.Y-parentRect.Top)
}
